package com.jaay.service;

import com.jaay.common.CommonResult;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

public abstract class BaseService<T, ID extends Serializable> implements IBaseService<T,ID> {


    private final MongoRepository<T,ID> mongoRepository;


    public BaseService(MongoRepository<T,ID> mongoRepository){

        this.mongoRepository = mongoRepository;

    }

    @Override
    public CommonResult<List<T>> queryAll() {

       return  new CommonResult<List<T>>().success(mongoRepository.findAll());

    }

    @Override
    public  Optional<T> queryById(ID id) {

        return mongoRepository.findById(id);
    }

    @Override
    public void add(T t) {

        mongoRepository.insert(t);

    }

    @Override
    public CommonResult<String> batchAdd(List<T> ts) {

        mongoRepository.insert(ts);

        return new CommonResult<String>().success();

    }

    @Override
    public void delete(ID id) {

        mongoRepository.deleteById(id);
    }

    @Override
    public void update(T t) {

        mongoRepository.save(t);
    }
}
